package evol

import org.apache.ivy.core.module.descriptor.ExtendsDescriptor;
import org.springframework.dao.DataIntegrityViolationException

class KorisnikController extends OsnovniController{

	static allowedMethods = [save: "POST", update: "POST", delete: "POST"]


	def beforeInterceptor = [action:this.&auth,except:[
			"login",
			"authenticate",
			"logout",
			"anon",
			"expert",
			"registracija",
			"register",
			"regUspesno",
			"regGreska"
		]]

	def index() {
		redirect(action: "list", params: params)
	}

	def list() {
		params.max = Math.min(params.max ? params.int('max') : 10, 100)
		[korisnikInstanceList: Korisnik.list(params), korisnikInstanceTotal: Korisnik.count()]
	}

	def create() {
		[korisnikInstance: new Korisnik(params)]
	}

	def save() {
		def korisnikInstance = new Korisnik(params)
		if (!korisnikInstance.save(flush: true)) {
			render(view: "create", model: [korisnikInstance: korisnikInstance])
			return
		}

		flash.message = message(code: 'default.created.message', args: [
			message(code: 'korisnik.label', default: 'Korisnik'),
			korisnikInstance.id
		])
		redirect(action: "show", id: korisnikInstance.id)
	}

	def show() {
		def korisnikInstance = Korisnik.get(params.id)
		if (!korisnikInstance) {
			flash.message = message(code: 'default.not.found.message', args: [
				message(code: 'korisnik.label', default: 'Korisnik'),
				params.id
			])
			redirect(action: "list")
			return
		}

		[korisnikInstance: korisnikInstance]
	}

	def edit() {
		def korisnikInstance = Korisnik.get(params.id)
		if (!korisnikInstance) {
			flash.message = message(code: 'default.not.found.message', args: [
				message(code: 'korisnik.label', default: 'Korisnik'),
				params.id
			])
			redirect(action: "list")
			return
		}

		[korisnikInstance: korisnikInstance]
	}

	def update() {
		def korisnikInstance = Korisnik.get(params.id)
		if (!korisnikInstance) {
			flash.message = message(code: 'default.not.found.message', args: [
				message(code: 'korisnik.label', default: 'Korisnik'),
				params.id
			])
			redirect(action: "list")
			return
		}

		if (params.version) {
			def version = params.version.toLong()
			if (korisnikInstance.version > version) {
				korisnikInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
						[
							message(code: 'korisnik.label', default: 'Korisnik')]
						as Object[],
						"Another user has updated this Korisnik while you were editing")
				render(view: "edit", model: [korisnikInstance: korisnikInstance])
				return
			}
		}

		korisnikInstance.properties = params

		if (!korisnikInstance.save(flush: true)) {
			render(view: "edit", model: [korisnikInstance: korisnikInstance])
			return
		}

		flash.message = message(code: 'default.updated.message', args: [
			message(code: 'korisnik.label', default: 'Korisnik'),
			korisnikInstance.id
		])
		redirect(action: "show", id: korisnikInstance.id)
	}

	def delete() {
		def korisnikInstance = Korisnik.get(params.id)
		if (!korisnikInstance) {
			flash.message = message(code: 'default.not.found.message', args: [
				message(code: 'korisnik.label', default: 'Korisnik'),
				params.id
			])
			redirect(action: "list")
			return
		}

		try {
			korisnikInstance.delete(flush: true)
			flash.message = message(code: 'default.deleted.message', args: [
				message(code: 'korisnik.label', default: 'Korisnik'),
				params.id
			])
			redirect(action: "list")
		}
		catch (DataIntegrityViolationException e) {
			flash.message = message(code: 'default.not.deleted.message', args: [
				message(code: 'korisnik.label', default: 'Korisnik'),
				params.id
			])
			redirect(action: "show", id: params.id)
		}
	}

	///------
	def login={
	}

	def authenticate={
		def korisnik=Korisnik.findByKorisnickoImeAndPass(params.user,params.password)

		if(korisnik){

			session.user=korisnik
			flash.message="Hello ${korisnik.korisnickoIme}"

			if(korisnik.role=="expert"){
				redirect(controller:"korisnik",action:"expert")
			}
			else {
				//redirect(controller:"predmet",action:"list")
				render(view:"admin")
			}
		}
		else{
			if(!params.user)
				params.user= "user"
			flash.message="Sorry,${params.user}. Please try again."
			redirect(action:"login")
		}
	}

	def logout ={
		if(session.user!=null)
			flash.message="Goodbye " +session.user
		session.user=null
		redirect(controller:"korisnik", action:"login")
	}


	def expert(){

		render (view : "anon")
	}

	def anon(){
		def ko=new Korisnik()
		ko.role="anon"
		ko.korisnickoIme= new Random().nextLong()
		session.user=ko
		render (view : "anon")
	}

	def registracija(){}
	
	def regGreska(){}
	
	def regUspesno(){}
	
	def register(){

		def k
		if(params.Password2!=params.Password1)
		{

			redirect(action:'regGreska',params:[note:'ne poklapaju se sifre'])
			return
		}
		k=Korisnik.findByKorisnickoIme(params.username)

		if(k){
			redirect(action:"regGreska",params:[note:"vec postoji sa takvim username- om"])
			return}
		k= new Korisnik()
		k.korisnickoIme=params.username
		k.pass=params.Password1
		k.role="expert"
		k.save()

		redirect(action:'regUspesno')

	}
}
